package com.htj.kf2b.construct_mobile.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.text.StrSplitter;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.htj.cross.util.Des;
import com.htj.cross.webservice.CrossService;
import com.htj.cross.webservice.CrossService_Service;
import org.apache.commons.collections.map.LinkedMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.tempuri.ArrayOfString;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/plan")
public class PlanController {

    /**
     *
     * @param planNumber
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "/plan", method = RequestMethod.GET)
    public JSONObject getPlan(String planNumber) {



        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.getPLanById","A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(planNumber);/*内外方法的参数*/
        data.getString ().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);
        String resultBase64 = Base64.decodeStr(r);
        JSONObject resultJsonObject = JSONUtil.parseObj(resultBase64);

        System.out.println(resultJsonObject);




//        System.out.println(planNumber);
        //TODO 通过planNumber 获取计划信息


        //模拟返回值
        LinkedMap map = new LinkedMap();
        map.put("name",resultJsonObject.get("type"));
        map.put("type",resultJsonObject.get("xh"));
        map.put("dayPlan",resultJsonObject.get("plandate"));
        map.put("date",resultJsonObject.get("doTime"));


        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOnce("code", "200");
        jsonObject.putOnce("msg", "成功");
        jsonObject.putAll(map);
        return jsonObject;
    }

    @CrossOrigin
    @RequestMapping(value = "/planList",method = RequestMethod.GET)
    public JSONObject getPlanList(String startDate ,String endDate,String phone){
        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.getCyclePlan","A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(startDate);/*内外方法的参数*/
        data.getString().add(endDate);/*内外方法的参数*/
        data.getString().add(phone);
        data.getString().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);
        String resultBase64 = Base64.decodeStr(r);
        String resultSting = resultBase64.substring(0, resultBase64.length() - 1);

//        System.out.println(resultSting);

        ArrayList arrayList = new ArrayList();


        List<String> split = StrSplitter.split(resultSting, '}', 0, true, true);
//        System.out.println(split);


        for (String s : split) {
//            System.out.println(s.substring(1)+"}");
            arrayList.add(JSONUtil.parseObj(s.substring(1)+"}"));
        }


//        System.out.println(arrayList);


        ArrayList list3 = new ArrayList();
        for (Object o : arrayList) {
            JSONObject jsonObject = new JSONObject(o);

            System.out.println(jsonObject.toString());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.putOnce("value",jsonObject.get("code"));
            jsonObject1.putOnce("label",jsonObject.get("code"));
            jsonObject1.putOnce("planName",jsonObject.get("type"));
            jsonObject1.putOnce("xh",jsonObject.get("xh"));
            jsonObject1.putOnce("dayPlan",jsonObject.get("plandate"));
            jsonObject1.putOnce("planDate",jsonObject.get("doTime"));

            list3.add(jsonObject1);
//        JSONObject jsonObject3 = new JSONObject();
//        jsonObject3.putOnce("label",o["code"]);
//        jsonObject3.putOnce("value",o.get("code"));
//        list3.add(jsonObject3);

        }
        System.out.println(list3);



        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOnce("code", "200");
        jsonObject.putOnce("msg", "成功");
        jsonObject.putOnce("trustee", arrayList);
        jsonObject.putOnce("planList",list3);
        return jsonObject;
    }


     @CrossOrigin
    @RequestMapping(value = "/dailyplanList",method = RequestMethod.GET)
    public String  getDailyPlanList(String date){
        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.getDailyPlan","A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(date);/*内外方法的参数*/
        data.getString().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);
        String resultBase64 = Base64.decodeStr(r);
        return resultBase64;
    }

    @CrossOrigin
    @RequestMapping(value = "/dailyplanListById",method = RequestMethod.GET)
    public String  getDailyPlanList(String date,String id){
        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.getDailyPlan","A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(date);/*内外方法的参数*/
        data.getString().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);
        String resultBase64 = Base64.decodeStr(r);
        return resultBase64;
    }

}